package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.pojo.mongo.FollowUser;
import com.itheima.commons.pojo.mongo.Video;
import com.itheima.dubbo.interfaces.CommentApi;
import com.itheima.dubbo.interfaces.VideoApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;

/**
 * @author yaohu
 * @date 2021/12/10
 * @Version 1.0
 * @describe
 */
@Service
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    CommentApi commentApi;

    /**
     * 保存小视频
     *
     * @param video
     * @return
     */
    @Override
    public String saveVideo(Video video) {
        // 需求  保存朋友圈-->生成的ID  朋友圈ID 保存到相册表  朋友的时间线表
        return mongoTemplate.save(video).getId().toHexString();
    }

    /**
     * 抽样小视频
     *
     * @param pageSize
     * @return
     */
    @Override
    public List<Video> randomVideos(Integer pageSize) {
        // 从mongodb中随机抽样数据
        AggregationResults<Video> aggregationResults = mongoTemplate.aggregate(
                Aggregation.newAggregation(Video.class, Aggregation.sample(Convert.toLong(pageSize))),
                Video.class);
        return aggregationResults.getMappedResults();
    }

    /**
     * 通过vids查询小视频
     *
     * @param vids
     * @return
     */
    @Override
    public List<Video> findVideoByVids(List<Long> vids) {
        return mongoTemplate.find(Query.query(Criteria.where("vid").in(vids)), Video.class);
    }

    /**
     * 查询是否关注
     *
     * @param userId       用户id
     * @param followUserId 主播id
     * @return
     */
    @Override
    public Boolean queryIsFollow(Long userId, Long followUserId) {
        List<FollowUser> followUserList = mongoTemplate.find(Query.query(Criteria
                .where("userId").is(userId)
                .and("followUserId").is(followUserId)), FollowUser.class);
        return CollUtil.isNotEmpty(followUserList);
    }

    /**
     * 根据小视频id查询小视频对象
     *
     * @param videoId
     * @return
     */
    @Override
    public Video queryVideoById(String videoId) {
        return mongoTemplate.findById(videoId, Video.class);
    }

    /**
     * 关注主播
     *
     * @param followUser
     */
    @Override
    public void followUser(FollowUser followUser) {
        mongoTemplate.save(followUser);
    }

    /**
     * 取消关注主播
     *
     * @param userId       用户id
     * @param followUserId 主播id
     */
    @Override
    public void disFollowUser(Long userId, Long followUserId) {
        mongoTemplate.remove(Query.query(Criteria
                .where("userId").is(userId)
                .and("followUserId").is(followUserId)
        ), FollowUser.class);
    }

    /**
     * 查询指定用户发布的所有视频列表
     *
     * @param uid       当前查看的用户的id
     * @return
     */
    @Override
    public List<Video> findByUserId( Long uid) {
        //构造条件
        Query query = new Query(Criteria.where("userId").is(uid));
        //按条件查询所有视频的详细信息集合
        List<Video> list = mongoTemplate.find(query, Video.class);
        return list;
    }

    /*@Override
    public Video findOneByUserId(String publishId) {
        return mongoTemplate.findById(publishId,Video.class);
    }*/

    @Override
    public Video findOneByUserId(String  videoId) {
        Query query = new Query(Criteria.where("id").is(videoId));
        Video video = mongoTemplate.findOne(query, Video.class);
        return video;
    }

    @Override
    public List<Video> findAll() {
        return mongoTemplate.findAll(Video.class);
    }


}
